<?php namespace Admin\Controller; 
use Hdphp\Controller\Controller;
//后台默认控制器
class IndexController extends CommonController{
	
    //后台首页
   	public function index(){
   	    View::make();
   	}
	//欢迎界面
	public function welcome(){
	    View::make();
	}
	
	//更改密码
	public function changePassword(){
		if(IS_POST){
			//1.判断密码至少是6位或者6位以上
			$newPassword = Q('post.newPassword');
			if(strlen($newPassword) < 6){
				View::error('新密码长度不足6位');
			}
			//2.两次密码是否相同
			$confirmPassword = Q('post.confirmPassword');
			if($newPassword != $confirmPassword){
				View::error('两次密码不一致');
			}
			//3.旧密码是否正确
			//得到用户提交的密码
			$password = Q('post.password','','md5');
			$model = new \Admin\Model\User;
			//通过session里面的uid找到当前用户数据库里面的数据
			$userData = $model->where(array('uid'=>$_SESSION['info']['uid']))->find();
			//p($userData);
//			Array
//			(
//			    [uid] => 1
//			    [username] => admin
//			    [password] => 21232f297a57a5a743894a0e4a801fc3
//			)
			if($userData['password'] != $password){
				View::error('旧密码错误');
			}
			//4.更改密码
			//UPDATE user SET password='加密的密码' WHERE uid=1;
			$model->where(array('uid'=>$_SESSION['info']['uid']))->save(array('password'=>md5($newPassword)));
			//5.清除session重新登录
			session_unset();
			session_destroy();
			$url = U('Login/index');
			$str = <<<str
<script>
parent.location.href = '{$url}';
</script>
str;
			exit($str);
		}
	    View::make();
	}
	
	
	
	
}
